package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference
    private ShippingService shippingService;
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        ShippingExample shippingExample = new ShippingExample();
        // 根据create_time降序排序
        shippingExample.setOrderByClause("create_time desc");
        shippingExample.createCriteria().andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Shipping> pageInfo = shippingService.findByPage(shippingExample, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "/cargo/shipping/shipping-list";
    }

    //预处理委托书
    @RequestMapping("/toAdd")
    public String toAdd(String id) {
        Shipping shipping = shippingService.toAddShipping(id);
        List<User> roleReviewer = userService.findRoleReviewer();
        request.setAttribute("shipping", shipping);
        request.setAttribute("reviewers", roleReviewer);
        return "/cargo/shipping/shipping-add";
    }

    //审核人来进行审核管理
    @RequestMapping("/toReviewer")
    public String toReviewer() {
        return "/cargo/shipping/shipping-reviewer";
    }

    //审核人审核
    @RequestMapping("/nowStartReviewer")
    public String nowStartReviewer(String id) {
        Shipping shipping = shippingService.toAddShipping(id);
        request.setAttribute("shipping",shipping);
        return "/cargo/shipping/shipping-view";
    }

    @RequestMapping("/edit")
    public String edit(Shipping shipping) {
        shipping.setCompanyId(getLoginCompanyId());
        shipping.setCompanyName(getLoginCompanyName());
        // 设置创建时间
        shipping.setCreateTime(new Date());
        // 创建人
        shipping.setCreateBy(getLoginUser().getId());
        // 创建人所属部门
        shipping.setCreateEdpt(getLoginUser().getDeptId());
        // 添加
        shippingService.save(shipping);
        return "redirect:/cargo/shipping/list";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        shippingService.delete(id);
        return "redirect:/cargo/shipping/list";
    }
}